package LuoGu.模拟与高精度.阶乘数码;

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        for(int i=0;i<n;i++){
            String temp = "";
            int ans = 0;
            int a = s.nextInt();
            String b = s.next();
            BigInteger temp1 = new BigInteger("1");
            for(int j=1;j<=a;j++){
                temp1 = temp1.multiply(new BigInteger(String.valueOf(j)));
            }
            temp = temp1.toString();
            for(int j=0;j<temp.length();j++){
                //System.out.println(b.charAt(0));
                if(temp.charAt(j)==b.charAt(0)) ans++;
            }
            System.out.println(ans);
        }

    }
}